// pages/circleSearch/circleSearch.js
import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { jump } from "../../utils/utils"

Page({

  /**
   * 页面的初始数据
   */
  data: {
     page: 1,
     total: 0,
     isDone: false,
     showDefault:true,
     search:'', //搜索值
     searchRecord:[], //搜索记录数据
     hotList:[], //热门圈子数据
     list:[] //搜索数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 搜索记录
    const searchRecord = wx.getStorageSync('searchRecord') || []
    this.setData({
      searchRecord
    })
    // 获取热门圈子
    this.getHotList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     this.modal= this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
 
  //搜索值
  searchInput(e){
    this.setData({
      search: e.detail.value
    })
  },
  //清空搜索值
  searchClear(){
    this.setData({
      search: '',
      showDefault: true,
      list:[],
    })
  },
  //点击搜索
  searchClick(){
    let {search , searchRecord} = this.data
    search=search.replace(/\s+/g,"")
    if(!search) return
    this.setData({
      isDone: false,
      page: 1,
      showDefault:false
    })
    this.getList(1)

   // 将历史记录添加到缓存中
    let keyword = search
    // 如果记录已存在，直接返回
    for (let i = 0; i < searchRecord.length; i++) {
      if (keyword == searchRecord[i].value) return
    }
      if (searchRecord.length >= 10){
        searchRecord.pop() // 删除最早的一条记录
      }
      searchRecord.unshift({
         value: keyword
       })
       this.setData({
        searchRecord
       })
       wx.setStorageSync('searchRecord', searchRecord)
  },
  // 点击搜索记录
  historySearch(e){
    this.setData({
       search: e.currentTarget.dataset.search
    })
    this.searchClick()
  },
  // 清空搜索记录
  clearSearchRecord(){
      this.modal.show()
  },
  clearRecordConfirm(){
     this.setData({
       searchRecord:[]
     })
     wx.setStorageSync('searchRecord', [])
     this.modal.hide()
  },

  // 获取热门圈子数据
  async getHotList(){
    const res= await request.get(api.circleList,{
      page:1,
      size: 6
   }, {loading: false})
   if(res.httpStatus==200){
      this.setData({
        hotList: res?.data?.records || [] ,
     })
   }
 },
  // 获取列表数据
 async getList(page){
    let search = this.data.search.replace(/\s+/g,"")
    const res= await request.get(api.circleList,{
      page,
      size: 10,
      title: search
   }, {loading: false})
  if(res.httpStatus==200){
   this.setData({
     list: page === 1 ? res?.data?.records : [
       ...this.data.list,
       ...res?.data?.records,
     ],
     total: res?.data?.pages
   })
   }
 },
   /**
   * 加入圈子
   */
  async joinClick(e){
    const res= await request.post(api.circleAdd + '/' + e.currentTarget.dataset.id, null, { loading: false })
   if(res.httpStatus == 200 ){
    setTimeout(() => {
      wx.showToast({
        icon: 'none',
        title: res.data
      })
    })
    let list= this.data.list
    if(res.data=='申请加入圈子成功'){
       list[e.currentTarget.dataset.index].status=1
    }else if(res.data=='加入圈子成功'){
       list[e.currentTarget.dataset.index].status=5
    }
    this.setData({
      list
    })
   }
  },
  //打开圈子详情页面
  jumpCircleDetail(e){
    jump('circle',{id: e.currentTarget.dataset.id})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 如果展示搜索记录、热门圈子不做操作
    if(this.showDefault) return
    let { page, total } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})